import multiprocessing
import time
import pkg_resources
from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


# 尝试获取 Appium Python 客户端的版本号
version = pkg_resources.get_distribution("Appium-Python-Client").version
print("Appium Python Client Version:", version)

# 获取尺寸的函数
def get_size(driver):
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    return (x, y)

def handle_appium(info):
    desired_caps = {
        "platformName": "Android",
        "platformVersion": "7.1.2",
        "deviceName": info['device'],
        "udid": info['device'],
        "appPackage": info['appPackage'],
        "appActivity": info['appActivity'],
        "noReset": True,
        "unicodekeyboard": True,
        "resetkeyboard": True,
        "automationName": "UiAutomator2"
    }

    remote_url = 'http://192.168.2.162:'+str(info['port'])
    driver = webdriver.Remote(remote_url, desired_caps)
    l = get_size(driver)
    x1 = int(l[0] * 0.5)
    y1 = int(l[1] * 0.15)
    y2 = int(l[1] * 0.9)

    #抖音
    if info['appPackage'] == 'com.ss.android.ugc.aweme':
        if WebDriverWait(driver, 60).until(lambda x: x.find_element_by_xpath("//android.widget.TextView[@text='首页']")):
            while True:
                # 初始鼠标位置，从哪开始，结束时鼠标位置，到哪结束
                driver.swipe(x1, y1, x1, y2)
                time.sleep(3)
    #快手
    elif info['appPackage'] == 'com.smile.gifmaker':
        if WebDriverWait(driver, 60).until(lambda x: x.find_element_by_xpath("//android.widget.ImageView[@resource-id='com.smile.gifmaker:id/logo']")):
            while True:
                # 初始鼠标位置，从哪开始，结束时鼠标位置，到哪结束
                driver.swipe(x1, y1, x1, y2)
                time.sleep(3)
    #今日头条  //*[@content-desc="推荐"]
    elif info['appPackage'] == 'com.ss.android.article.news':
        try:
            element = WebDriverWait(driver, 10, 0.5).until(
                EC.presence_of_element_located((By.XPATH, "//*[@content-desc='推荐']"))
            )
            # 如果找到元素，执行后续操作
            element.click()
            while True:
                driver.swipe(x1, y2, x1, y1)
                time.sleep(3)            
        except Exception as e:
            print("元素未找到:", e)



if __name__ == '__main__':
    m_list = []
    #定义两台虚拟设备
    devices_list = [
        # {
        #     "device":"192.168.2.117:5555",
        #     "appPackage": "com.ss.android.ugc.aweme",
        #     "appActivity": "com.ss.android.ugc.aweme.splash.SplashActivity",
        #     "port": 4723,
        #     "key": '抖音'
        # }
        # ,
        # {
        #     "device":"192.168.2.202:5555",
        #     "appPackage": "com.smile.gifmaker",
        #     "appActivity": "com.yxcorp.gifshow.HomeActivity",
        #     "port": 4724,
        #     "key": '快手'
        # },
        {
            "device":"192.168.2.221:5555",
            "appPackage": "com.ss.android.article.news",
            "appActivity": "com.ss.android.article.news.activity.MainActivity",
            "port": 4725,
            "key": '今日头条'
        },
    ]
    for device in (devices_list):
        m_list.append(multiprocessing.Process(target=handle_appium,args=(device,)))

    for m1 in m_list:
        m1.start()

    for m2 in m_list:
        m2.join()
